package com.modebing.es.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class ESTest_doc_update {

    public static void main(String[] args) throws IOException {
        // 创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //修改数据
        UpdateRequest request = new UpdateRequest();

        // 向es插入数据，必须是json数据
        request.index("user").id("1001").doc(XContentType.JSON,"sex","女");
        UpdateResponse indexResponse = esClient.update(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());
        System.out.println(indexResponse.getId());
        // 关闭客户端
        esClient.close();
    }
}
